<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="showRoleDialog"
          >新增角色</el-button>
          <el-dialog title="编辑弹层" :visible="roleDialogVisible" @close="closeRoleDialog">
            <!-- 表单内容 -->
           <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="roleForm.name" />
              </el-form-item>
              <el-form-item label="角色描述" prop="description">
                <el-input v-model="roleForm.description" />
              </el-form-item>
            </el-form>
            <!-- 底部 -->
            <el-row slot="footer" type="flex" justify="center">
              <el-button size="small" @click="closeRoleDialog">取消</el-button>
              <el-button size="small" type="primary" @click="confirmAdd">确定</el-button>
            </el-row>
          </el-dialog>
        </el-row>
        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column label="序号" width="120" type="index" />
          <el-table-column label="角色名称" width="240" prop="name"/>
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作" width="240" fixed="right">
            <template #default="{ row }">
                <el-button size="small">分配权限</el-button>
                <el-button size="small" @click="editRole(row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination 
          layout="prev,pager,next" 
          :current-page="params.page"
          :page-size="params.pagesize"
          :total="total"
          @current-change="pageChange"
          />
        </el-row>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail } from '@/api/setting'
export default {
  name: 'Setting',
  data() {
    return {
      params: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      roleList: [],
      roleDialogVisible: false,
      roleForm:{
        name: '',
        description: ''
      },
      rules:{
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.fetchRoleList()
  },
  methods: {
    // 获取数据列表
    async fetchRoleList() {
      const {rows, total} = await getRoleList(this.params)
      this.roleList = rows
      this.total = total
      console.log(rows)
    },
    // 分页
    pageChange(page) {
      this.params.page = page
      this.fetchRoleList()
    },
    // 删除按钮
    delRole(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 调用删除接口
        await deleteRole(id)
        // 更新列表
        this.fetchRoleList()
        // 提示用户
        this.$message.success('删除角色成功')
      }).catch()
    },
    // 打开窗口
    showRoleDialog() {
      this.roleDialogVisible = true
    },
    // 关闭窗口
    closeRoleDialog() {
      this.roleDialogVisible = false
    },
    confirmAdd() {
      this.$refs.roleForm.validate(async valid => {
        if (valid) {
          await addRole(this.roleForm)
          // 刷新列表
          this.fetchRoleList()
          // 告知用户
          this.$message.success('新增成功')
          // 清空表单
          this.roleForm = {
            name: '',
            description: ''
          }
          this.roleDialogVisible = false
        }
      })
    },
    // 取消
    closeRoleDialog() {
      // 清空表单数据
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.roleForm.resetFields()
      // 关闭弹框
      this.roleDialogVisible = false
    },
    // 编辑
    async editRole(id) {
      const res = await getRoleDetail(id)
      this.roleForm = res
      this.roleDialogVisible = true
    }
  }
}
</script>
